﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public partial class DataManager : MonoBehaviour
{   // Application Face Data
    List<string> listFaceName = new List<string>();
    Sprite[] FaceSprites;

    public void SetFaceData()
    {

        string strFaceName;
        FaceSprites = Resources.LoadAll<Sprite>("Sprite/Face");

        for (int i = 0; i < FaceSprites.Length; ++i)
        {
            strFaceName = string.Format("face{0}", i);
            listFaceName.Add(strFaceName);
        }
    }

    public string GetFaceData()
    {
        if (listFaceName.Count == 0)  // 얼굴 다썻으면 다시 얼굴한번돌림
            SetFaceData();

        int iTemp = Random.Range(0, listFaceName.Count);
        string strTemp = listFaceName[iTemp];
        listFaceName.RemoveAt(iTemp);

        return strTemp;
    }
}

public partial class DataManager : MonoBehaviour
{   // BuildData
    public List<BuildData>[] listBuildData = new List<BuildData>[2];
    List<WeaponData>[] listWeaponData = new List<WeaponData>[(int)WEAPON_TYPE.END];

    public void Set_WeaponData_List()
    {
        for (int i = 0; i < (int)WEAPON_TYPE.END; ++i)
        {
            listWeaponData[i] = new List<WeaponData>();
        }
        for (int i = 0; i < xmlLoader.GetInstance().m_XML_Weapons.Count; ++i)
        {
            Dictionary<string, string> tempDic = xmlLoader.GetInstance().m_XML_Weapons[i];

            WeaponData pWeaponData = new WeaponData();

            tempDic.TryGetValue("Name", out pWeaponData.m_strWeaponName);

            string tempGrade;
            tempDic.TryGetValue("Grade", out tempGrade);
            pWeaponData.m_WeaponGrade = System.Convert.ToInt32(tempGrade);

            string tempAccur;
            tempDic.TryGetValue("Accuracy", out tempAccur);
            pWeaponData.m_iWeaponAccur = System.Convert.ToInt32(tempAccur);

            string tempDmg;
            tempDic.TryGetValue("Damage", out tempDmg);
            pWeaponData.m_iWeaponDmg = System.Convert.ToInt32(tempDmg);

            string tempRate;
            tempDic.TryGetValue("Rate", out tempRate);
            pWeaponData.m_iWeaponRate = System.Convert.ToInt32(tempRate);

            string tempAmmo;
            tempDic.TryGetValue("Ammo", out tempAmmo);
            pWeaponData.m_iFullAmmo = System.Convert.ToInt32(tempAmmo);
            pWeaponData.m_iCurrentAmmo = pWeaponData.m_iFullAmmo;

            string tempPrice;
            tempDic.TryGetValue("Price", out tempPrice);
            pWeaponData.m_iPrice = System.Convert.ToInt32(tempPrice);

            string tempType;
            tempDic.TryGetValue("Type", out tempType);
            switch (tempType)
            {
                case "Close":
                    pWeaponData.m_WeaponType = WEAPON_TYPE.CLOSE;
                    break;
                case "Pistol":
                    pWeaponData.m_WeaponType = WEAPON_TYPE.PISTOL;
                    break;
                case "Rifle":
                    pWeaponData.m_WeaponType = WEAPON_TYPE.RIFLE;
                    break;
            }

            listWeaponData[(int)pWeaponData.m_WeaponType].Add(pWeaponData);
        }
    }

    public void SetBuildData()
    {
        BuildData buildData;

        listBuildData[0] = new List<BuildData>();
        listBuildData[1] = new List<BuildData>();

        for (int i = 0; i < xmlLoader.GetInstance().m_XML_Buildings.Count; ++i)
        {
            string temp;

            buildData = new BuildData();

            xmlLoader.GetInstance().m_XML_Buildings[i].TryGetValue("id", out temp);
            buildData.iID = System.Convert.ToInt32(temp);

            xmlLoader.GetInstance().m_XML_Buildings[i].TryGetValue("SpriteName", out buildData.strSpriteName);

            xmlLoader.GetInstance().m_XML_Buildings[i].TryGetValue("Name", out buildData.strName);

            xmlLoader.GetInstance().m_XML_Buildings[i].TryGetValue("BuildTime", out temp);
            buildData.fBuildTime = System.Convert.ToSingle(temp);

            xmlLoader.GetInstance().m_XML_Buildings[i].TryGetValue("Price", out temp);
            buildData.iPrice = System.Convert.ToInt32(temp);

            xmlLoader.GetInstance().m_XML_Buildings[i].TryGetValue("Desc", out buildData.strExplain);

            xmlLoader.GetInstance().m_XML_Buildings[i].TryGetValue("HP", out temp);
            buildData.iHP = System.Convert.ToInt32(temp);
            buildData.fCurrentHP = buildData.iHP;

            xmlLoader.GetInstance().m_XML_Buildings[i].TryGetValue("Size", out temp);

            string strSizeX, strSizeY;
            strSizeX = temp.Substring(0, temp.IndexOf("x"));
            strSizeY = temp.Substring(temp.IndexOf("x") + 1, temp.Length -  temp.IndexOf("x") - 1);
            buildData.vSize = new Vector2(System.Convert.ToInt32(strSizeX), System.Convert.ToInt32(strSizeY));

            

            xmlLoader.GetInstance().m_XML_Buildings[i].TryGetValue("Type", out temp);
            if (temp == "Cover")
            {
                buildData.enType = BUILD_TYPE.COVER;
                listBuildData[0].Add(buildData);
            }
            else
            {
                buildData.enType = BUILD_TYPE.BUILDING;
                listBuildData[1].Add(buildData);
            }
        }
    }

    public BuildData GetBuildData(BUILD_TYPE enType, int ID)
    {
        return (listBuildData[(int)enType])[ID];
    }

    public List<BuildData> GetBuildDatas(BUILD_TYPE enType)
    {
        return listBuildData[(int)enType];
    }
}

public partial class DataManager : MonoBehaviour
{   // ClassData
    List<ClassData> listClassData = new List<ClassData>();

    public void SetClassData()
    {
        listClassData.Add(ClassDataFactory(CLASS_TYPE.TECHNICIAN, xmlLoader.GetInstance().TextLoad(XML_INDEX.CLASS_DEF, "Technician"),
                                             ASSN_TYPE.DEFAULT, xmlLoader.GetInstance().TextLoad(XML_INDEX.ASSN_DEF, "Engineer"), xmlLoader.GetInstance().TextLoad(XML_INDEX.ASSN_DEF, "EngineerDesc"), 200,
                                             0));
        listClassData.Add(ClassDataFactory(CLASS_TYPE.TECHNICIAN, xmlLoader.GetInstance().TextLoad(XML_INDEX.CLASS_DEF, "Technician"),
                                             ASSN_TYPE.RANGE, xmlLoader.GetInstance().TextLoad(XML_INDEX.ASSN_DEF, "Medic"), xmlLoader.GetInstance().TextLoad(XML_INDEX.ASSN_DEF, "MedicDesc"), 200,
                                             0));

        listClassData.Add(ClassDataFactory(CLASS_TYPE.POLICE, xmlLoader.GetInstance().TextLoad(XML_INDEX.CLASS_DEF, "Police"),
                                             ASSN_TYPE.DEFAULT, xmlLoader.GetInstance().TextLoad(XML_INDEX.ASSN_DEF, "Recruit"), xmlLoader.GetInstance().TextLoad(XML_INDEX.ASSN_DEF, "RecruitDesc"), 0,
                                             0));
        listClassData.Add(ClassDataFactory(CLASS_TYPE.POLICE, xmlLoader.GetInstance().TextLoad(XML_INDEX.CLASS_DEF, "Police"),
                                             ASSN_TYPE.RANGE, xmlLoader.GetInstance().TextLoad(XML_INDEX.ASSN_DEF, "Officer"), xmlLoader.GetInstance().TextLoad(XML_INDEX.ASSN_DEF, "OfficerDesc"), 50,
                                             0));
        listClassData.Add(ClassDataFactory(CLASS_TYPE.POLICE, xmlLoader.GetInstance().TextLoad(XML_INDEX.CLASS_DEF, "Police"),
                                             ASSN_TYPE.CLOSE, xmlLoader.GetInstance().TextLoad(XML_INDEX.ASSN_DEF, "Suppressor"), xmlLoader.GetInstance().TextLoad(XML_INDEX.ASSN_DEF, "SuppressorDesc"), 50,
                                             0));

        listClassData.Add(ClassDataFactory(CLASS_TYPE.SOLDIER, xmlLoader.GetInstance().TextLoad(XML_INDEX.CLASS_DEF, "Army"),
                                             ASSN_TYPE.DEFAULT, xmlLoader.GetInstance().TextLoad(XML_INDEX.ASSN_DEF, "Soldier"), xmlLoader.GetInstance().TextLoad(XML_INDEX.ASSN_DEF, "SoldierDesc"), 0,
                                             0));
        listClassData.Add(ClassDataFactory(CLASS_TYPE.SOLDIER, xmlLoader.GetInstance().TextLoad(XML_INDEX.CLASS_DEF, "Army"),
                                             ASSN_TYPE.RANGE, xmlLoader.GetInstance().TextLoad(XML_INDEX.ASSN_DEF, "Ranger"), xmlLoader.GetInstance().TextLoad(XML_INDEX.ASSN_DEF, "RangerDesc"), 100,
                                             0));
        listClassData.Add(ClassDataFactory(CLASS_TYPE.SOLDIER, xmlLoader.GetInstance().TextLoad(XML_INDEX.CLASS_DEF, "Army"),
                                             ASSN_TYPE.CLOSE, xmlLoader.GetInstance().TextLoad(XML_INDEX.ASSN_DEF, "Assult"), xmlLoader.GetInstance().TextLoad(XML_INDEX.ASSN_DEF, "ArmyDesc"), 100,
                                             0));
    }

    public ClassData GetClassData(CLASS_TYPE enClassType, ASSN_TYPE enAssnType, int iGrade)
    {

        for (int i = 0; i < listClassData.Count; ++i)
        {
            ClassData TempClassData = listClassData[i];

            if (TempClassData.enClass == enClassType && TempClassData.AssnStatus.enAssnType == enAssnType
                && TempClassData.AssnStatus.iRank == iGrade)
                return TempClassData;
        }

        return null;
    }

    ClassData ClassDataFactory(CLASS_TYPE enClassType, string strClassName, ASSN_TYPE enAssnType, string strAssnName, string strAssnExplain, int iAssnTrainPrice, int iGrade)
    {
        ClassData BackClassData = new ClassData();

        BackClassData.enClass = enClassType;
        BackClassData.strClassName = strClassName;
        BackClassData.AssnStatus = new AssnData();
        BackClassData.AssnStatus.enAssnType = enAssnType;
        BackClassData.AssnStatus.strAssnName = strAssnName;
        BackClassData.AssnStatus.strAssnExplain = strAssnExplain;
        BackClassData.AssnStatus.iAssnTrainPrice = iAssnTrainPrice;
        BackClassData.AssnStatus.iRank = iGrade;

        return BackClassData;
    }

    public WeaponData Get_WeaponData_List(WEAPON_TYPE Type, string WpnName)
    {
        for (int i = 0; i < listWeaponData[(int)Type].Count; ++i)
        {
            if ((listWeaponData[(int)Type])[i].m_strWeaponName == WpnName)
                return (listWeaponData[(int)Type])[i];

        }
        return (listWeaponData[(int)Type])[0];
    }
}

public partial class DataManager : MonoBehaviour  //Perk Data
{
    public List<PerkData> m_ListPerkData = new List<PerkData>();

    bool m_bPersonalityEnd;
    int m_iPersonalityPerkIndex;

    public void SetPerkData()
    {
        int i = 0;
        PerkData tempPerk = new PerkData();

        foreach (KeyValuePair<string, string> dicNode in xmlLoader.GetInstance().m_XML_Defs[(int)XML_INDEX.PERK_DEF])
        {
            if (!dicNode.Key.Contains("Desc"))
            {
                tempPerk = new PerkData();

                if (dicNode.Key.Contains("Personality"))
                    tempPerk.m_enCategory = PERK_CATEGORY.PERSONALITY;
                else if (dicNode.Key.Contains("PTSD"))
                {
                    tempPerk.m_enCategory = PERK_CATEGORY.PTSD;

                    if (!m_bPersonalityEnd)
                    {
                        m_iPersonalityPerkIndex = i / 2;
                        m_bPersonalityEnd = true;
                    }
                }
                else if (dicNode.Key.Contains("Injury"))
                    tempPerk.m_enCategory = PERK_CATEGORY.INJURY;

                tempPerk.m_strName = dicNode.Value;
            }
            else
            {
                
                tempPerk.m_iPerkID = i / 2;
                tempPerk.m_strDesc = dicNode.Value;

                if (tempPerk.m_strDesc != null)
                {
                    tempPerk.m_iHealth = StatInitializing(tempPerk.m_strDesc, "Health");
                    tempPerk.m_iMorale = StatInitializing(tempPerk.m_strDesc, "Morale");
                    tempPerk.m_iSpeed = StatInitializing(tempPerk.m_strDesc, "Speed");
                    tempPerk.m_iAccuracy = StatInitializing(tempPerk.m_strDesc, "Accuracy");
                    tempPerk.m_iStrength = StatInitializing(tempPerk.m_strDesc, "Strength");
                }

                m_ListPerkData.Add(tempPerk);
            }

            ++i;
        }
    }

}


public partial class DataManager : MonoBehaviour  //Trait Data
{
    public List<TraitData> m_ListTraitData = new List<TraitData>();

    public void SetTraitData()
    {
        for (int i = 0; i < xmlLoader.GetInstance().m_XML_Traits.Count; ++i)
        {
            TraitData tempTrait = new TraitData();

            string tempID;
            xmlLoader.GetInstance().m_XML_Traits[i].TryGetValue("id", out tempID);
            tempTrait.m_iID = System.Convert.ToInt32(tempID);

            xmlLoader.GetInstance().m_XML_Traits[i].TryGetValue("SpriteName", out tempTrait.m_strSpriteName);
            xmlLoader.GetInstance().m_XML_Traits[i].TryGetValue("Name", out tempTrait.m_strName);
            xmlLoader.GetInstance().m_XML_Traits[i].TryGetValue("Desc", out tempTrait.m_strDesc);

            tempTrait.m_iHealth = StatInitializing(tempTrait.m_strDesc, "Health");
            tempTrait.m_iMorale = StatInitializing(tempTrait.m_strDesc, "Morale");
            tempTrait.m_iSpeed = StatInitializing(tempTrait.m_strDesc, "Speed");
            tempTrait.m_iAccuracy = StatInitializing(tempTrait.m_strDesc, "Accuracy");
            tempTrait.m_iStrength = StatInitializing(tempTrait.m_strDesc, "Strength");

            m_ListTraitData.Add(tempTrait);
        }
    }

    int StatInitializing(string strDesc, string strKey)
    {
        int iTempCounter = 0;

        for (int i = 0; i < strDesc.IndexOf(strKey); ++i)
        {
            if (strDesc[i] == '\n')
                iTempCounter = i+1;
        }

        while (strDesc.IndexOf(strKey, iTempCounter) != -1)
        {
            if (strDesc.IndexOf("\n", iTempCounter) != -1) // 다음줄이 있을때
            {
                for (int j = strDesc.IndexOf(strKey, iTempCounter); j < strDesc.IndexOf("\n", iTempCounter); ++j)
                {
                    if (strDesc[j] == '+')
                    {
                        int jj = strDesc.IndexOf("\n", iTempCounter) - 1;
                        string a = strDesc.Substring(j + 1, strDesc.IndexOf("\n", iTempCounter) - 1 - (j + 1));
                        return System.Convert.ToInt32(strDesc.Substring(j + 1, strDesc.IndexOf("\n", iTempCounter) - 1 - (j + 1)));
                    }
                    else if (strDesc[j] == '-')
                    {
                        return System.Convert.ToInt32(strDesc.Substring(j + 1, strDesc.IndexOf("\n", iTempCounter) - 1 - (j + 1))) * -1;
                    }
                }
                iTempCounter = strDesc.IndexOf("\n", iTempCounter) +1;
            }
            else //마지막줄
            {
                for (int j = strDesc.IndexOf(strKey, iTempCounter); j < strDesc.Length; ++j)
                {
                    if (strDesc[j] == '+')
                    {
                        return System.Convert.ToInt32(strDesc.Substring(j + 1, strDesc.Length - j -1 ));
                    }
                    else if (strDesc[j] == '-')
                    {
                        return System.Convert.ToInt32(strDesc.Substring(j + 1, strDesc.Length - j -1)) * -1;
                    }
                }

                break;
            }
        }

        return 0;
    }

    public TraitData GetTraitData(string strName)
    {
        for (int i = 0; i < m_ListTraitData.Count; ++i)
        {
            if (m_ListTraitData[i].m_strSpriteName == strName)
                return m_ListTraitData[i];
        }
        return null;
    }
}

public partial class DataManager : MonoBehaviour
{ //Hire Data
    public List<UnitData> HireList = new List<UnitData>();

    UnitData RandHireList(ASSN_TYPE AssnType = ASSN_TYPE.END, CLASS_TYPE ClassType = CLASS_TYPE.END)
    {

        UnitData unitData = new UnitData();

        unitData.strFirstName = xmlLoader.GetInstance().TextLoad(XML_INDEX.FIRST_NAME_DEF, string.Format("FirstName{0}", Random.Range(0, xmlLoader.GetInstance().m_XML_Defs[(int)XML_INDEX.FIRST_NAME_DEF].Count)));   // 이름설정 
        unitData.strLastName = xmlLoader.GetInstance().TextLoad(XML_INDEX.LAST_NAME_DEF, string.Format("LastName{0}", Random.Range(0, xmlLoader.GetInstance().m_XML_Defs[(int)XML_INDEX.LAST_NAME_DEF].Count)));
        unitData.strNickName = "";

        unitData.iSoldierType = 1;
        unitData.strFaceName = GameObject.Find("DataLogic").GetComponent<DataManager>().GetFaceData();

        unitData.iChaos = 0;

        unitData.iLevel = 1;
        unitData.iShootSpeed = Random.Range(5, 10);
        unitData.iAtt = 0;
        unitData.iDef = 0;
        unitData.iHP = Random.Range(5, 10);
        unitData.iAccur = Random.Range(5, 10);
        unitData.iStrength = Random.Range(5, 10);
        unitData.iMorale = Random.Range(5, 10);

        unitData.iSpeedStat = (int)(Random.Range(5, 10));
        unitData.fSpeed = unitData.iSpeedStat * 0.1f;
        unitData.iDetect = Random.Range(5, 10);
        unitData.fChatty = Random.Range(0.25f, 2.0f);

        for (int i = 0; i < 2; ++i)
        {
            int iRand;///////////////////////////RandChecker/////////////////////////////////////////RandChecker/////////////////////////////////////////RandChecker/////////////////////////////////////////RandChecker//////////////
            while (true)
            {
                bool bRepeat = false;

                iRand = Random.Range(0, m_iPersonalityPerkIndex);

                for (int j = 0; j < unitData.ListPerk.Count; ++j)
                {
                    if (unitData.ListPerk[j].m_iPerkID == iRand)
                        bRepeat = true;
                }

                if (bRepeat)
                    continue;

                unitData.ListPerk.Add(m_ListPerkData[iRand]);
                break;
            }
            /////////////////////////RandChecker/////////////////////////////////////////RandChecker/////////////////////////////////////////RandChecker/////////////////////////////////////////RandChecker//////////////
        }

        unitData.iCurrentHP = 100;
        unitData.iCurrentMorale = 50;

        unitData.bTalking = false;

        if (ClassType == CLASS_TYPE.END)  // 클래스랜덤
        {
            switch (Random.Range(0, 2))
            {
                case 0:
                    unitData.ClassStatus = GameObject.Find("DataLogic").GetComponent<DataManager>().GetClassData(CLASS_TYPE.TECHNICIAN, ASSN_TYPE.DEFAULT, 0);
                    break;

                case 1:
                    unitData.ClassStatus = GameObject.Find("DataLogic").GetComponent<DataManager>().GetClassData(CLASS_TYPE.POLICE, ASSN_TYPE.DEFAULT, 0);
                    break;
            }
        }
        else
        {
            switch (ClassType)
            {
                case CLASS_TYPE.TECHNICIAN:
                    unitData.ClassStatus = GameObject.Find("DataLogic").GetComponent<DataManager>().GetClassData(CLASS_TYPE.TECHNICIAN, AssnType, 0);
                    break;

                case CLASS_TYPE.POLICE:
                    unitData.ClassStatus = GameObject.Find("DataLogic").GetComponent<DataManager>().GetClassData(CLASS_TYPE.POLICE, AssnType, 0);
                    break;

                case CLASS_TYPE.SOLDIER:
                    break;
            }
        }
        

        unitData.arrayTrait = new TraitData[4];


        return unitData;
    }

    public UnitData MakeOneRandUnit(ASSN_TYPE Asn, CLASS_TYPE Cls)
    {
        return RandHireList(Asn, Cls);
    }


    public void ResetHireList()
    {
        HireList.Clear();
        for (int i = 0; i < 6; ++i)
        {
            HireList.Add(RandHireList());
        }

    }

}
